package com.example.web;

import com.example.business.RecordsService;
import com.example.business.UserService;
import com.example.data.DataUtils;
import com.example.data.model.Records;
import com.example.exception.ValidateException;
import com.example.page.Page;
import com.example.page.Pageable;
import com.example.utils.AuthToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by hldev on 17-2-9.
 */
@RestController
@RequestMapping("/demo/records")
@Api(value = "api-records", description = "论坛功能")
public class RecordsController {

    private final RecordsService recordsService;

    private final UserService userService;

    @Autowired
    public RecordsController(RecordsService recordsService, UserService userService) {
        this.recordsService = recordsService;
        this.userService = userService;
    }

    @GetMapping(path = "{id}")
    @ApiOperation(value = "根据id查找消息", notes = "根据id查找消息")
    public Records getOne(@PathVariable("id") Integer id) {
        return recordsService.findOne(id);
    }

    @GetMapping(path = "list")
    @ApiOperation(value = "查找所有消息", notes = "查找所有消息")
    public List<Records> getAll() {
        return recordsService.findAll();
    }


    @PostMapping
    @ApiOperation(value = "创建一条消息", notes = "创建一条消息")
    public Records createRecords(@RequestBody Records records) {
//        records.setCreateName(AuthToken.getFromThreadLocal().get().userName);
//        records.setCreateBy(userService.findByUserName(AuthToken.getFromThreadLocal().get().userName).getId());
        records.setCreateDate(LocalDateTime.now());
        recordsService.createRecords(records);
        return records;
    }

    @GetMapping(path = "page")
    @ApiOperation(value = "查找所有消息并分页", notes = "查找所有消息并分页")
    public Page<Records> findPage(
            @RequestParam(name = "parentRecordId", required = false) Integer parentRecordId,
            @RequestParam(name = "parentCreateBy", required = false) Integer parentCreateBy,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "title", required = false) String title,
            @RequestParam(name = "createName", required = false) String createName,
            @RequestParam(name = "beginDate", required = false) String beginDate,
            @RequestParam(name = "endDate", required = false) String endDate,
            @RequestParam(value = "page", required = false, defaultValue = Pageable.STR_PAGE_INDEX) Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "5") Integer size,
            @RequestParam(value = "orderField", required = false, defaultValue = "CREATE_DATE") String orderField,
            @RequestParam(value = "direction", required = false, defaultValue = "DESC") String direction) {
        Pageable pageable = DataUtils.pageable(page, size, orderField, direction);
        return recordsService.findPage(pageable, parentRecordId, parentCreateBy, status, title, createName, beginDate, endDate);
    }

    @PutMapping
    @ApiOperation(value = "更新状态", notes = "更新状态")
    public Records setStatus(@RequestBody @Valid Records records, BindingResult result) {
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
        recordsService.setStatus(records);
        return records;
    }

    @DeleteMapping(path = "{id}")
    @ApiOperation(value = "删除", notes = "删除")
    public ResponseEntity delete(@PathVariable Integer id) {
        recordsService.deleteRecords(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
